/**
 *
 */
package forum;

import java.util.Date;

import java.util.List;
import database.DBHandler;

/**
 * @author Olga
 * 
 */
public class ForumsHandler {
	
	private DBHandler dbHandler;

	public ForumsHandler(DBHandler dbHandler) {
		this.dbHandler = dbHandler;
	}

	/**
	 * 
	 * @param subject
	 * @return ID of added forum with given subject <br>
	 *         in case of error, returns -1
	 */
	public synchronized int addForum(String subject) {
		int res = dbHandler.addForumGui(subject);

		if (res != 0)
			return res;
		return -1;
	}

	/**
	 * 
	 * @param forumID
	 * @param subject
	 * @param content
	 * @param author
	 * @return id of root post of added thread <br>
	 *         in case of error return -1a
	 */
	public synchronized int addThread(int forumID, String subject,
			String content, String author) {

		Date d = new Date();
		int res = dbHandler.addThreadGui(author, forumID,
				subject, content, d.toString());

		if (res > 0)
			return res;

		return -1;
	}

	/**
	 * 
	 * @param forumID
	 * @param postID
	 * @param subject
	 * @param content
	 * @param author
	 * @return ID of added post. <br>
	 *         in case of error returns -1
	 */
	public synchronized int addReply(int forumID, int postID, String subject,
			String content, String author) {

		Date d = new Date();
		int res = dbHandler.addReplyGui(postID, author, subject,
				content, d.toString());

		if (res > 0)
			return res;
		return -1;
	}

	/**
	 * 
	 * @param postID
	 * @param content
	 *            - new content of post
	 * @return true on successful edit, false else
	 */
	public synchronized boolean editPost(int postID, String content) {
		int res = dbHandler.editPost(postID, content);

		if (res == 1)
			return true;
		return false;
	}

	/**
	 * @param postID
	 * @return true on success, false else
	 */
	public synchronized boolean removePost(int postID) {
		int res = dbHandler.removeReply(postID);

		if (res == 1)
			return true;
		return false;
	}

	/**
	 * 
	 * @param rootPostID
	 * @return true on success, false else
	 */
	public synchronized boolean removeThread(int rootPostID) {
		int res = dbHandler.removeThread(rootPostID);

		if (res == 1)
			return true;

		return false;
	}

	/**
	 * 
	 * @param forumID
	 * @return true on success, false else
	 */
	public synchronized boolean removeForum(int forumID) {
		int res = dbHandler.removeForum(forumID);

		if (res == 1)
			return true;
		return false;
	}

	/**
	 * 
	 * @param forumID
	 * @return Forum object with ID forumID if exist, else return null
	 */
	public synchronized Forum getForum(int forumID) {
		List<Forum> forums = dbHandler.getForums();
		for (Forum f : forums)
			if (f.getID() == forumID) {
				return f;
			}
		return null;
	}

	/**
	 * 
	 * @param rootPostID
	 * @return reference to thread with rootID = rootPostID. If such thread not
	 *         found, return null
	 */
	public synchronized Thread getThread(int rootPostID) {
		Thread ans = null;
		Post root = dbHandler.getPost(rootPostID);
		if (root == null) {
			return null;
		}
		ans = new Thread(rootPostID, root.getSubject(), root.getContent(),
				root.getAuthor());
		return ans;
	}

	/**
	 * 
	 * @param postID
	 * @return Post object with postID. If not found, returns null
	 */
	public synchronized Post getPost(int postID) {
		Post ans = dbHandler.getPost(postID);
		return ans;
	}

	/**
	 * 
	 * @return only topics of forums
	 */

	public synchronized List<Forum> getForums() {
		return dbHandler.getForums();
	}

	/**
	 * 
	 * @return list of ID's of root post of all threads
	 */
	public synchronized List<Integer> getThreadsIDs() {
		return dbHandler.getThreadsIds();
	}
}
